package com.woniu.zsd.platform.interceptor;


import com.woniu.zsd.common.util.JWTUtil;
import com.woniu.zsd.common.util.RedisKey;
import com.woniu.zsd.platform.annotation.RequirePermission;
import com.woniu.zsd.platform.annotation.RequireRole;
import jodd.util.StringUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;

@Component
public class PermissionInterceptor implements HandlerInterceptor {
    @Resource
    private JWTUtil jwtUtil;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate<String, Object> redisObjectTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
            return true;
        }

        //取出token中的角色信息
        String token = request.getHeader("token");
        if(token == null){
            throw new Exception("token为空");
        }
        String refreshToken = stringRedisTemplate.opsForValue().get(RedisKey.ACCESS_REFRESH_TOKEN2(token));
        if(jwtUtil.parseToken(refreshToken).get("roleName") == null){
            throw new Exception("请先分配角色");
        }
        String role = jwtUtil.parseToken(refreshToken).get("roleName").toString();
        Method method = ((HandlerMethod) handler).getMethod();

        // 取出当前请求要执行的目标方法上的注解
        if (method.isAnnotationPresent(RequireRole.class)) {
            RequireRole requireRole = method.getAnnotation(RequireRole.class);
            String[] requireRoles = requireRole.value();
            // 检查角色是否匹配
            for(String requireRole2 : requireRoles){
                if (requireRole2.equals(role)) {
                    return true;  // 角色匹配，放行
                }
            }
            // 角色不匹配时，继续判断该角色是否有权限
            if (method.isAnnotationPresent(RequirePermission.class)) {
                String requirePermission = method.getAnnotation(RequirePermission.class).value();
                Integer accountId = jwtUtil.getAccountId(token);
                List<String> permissions = (List<String>) redisObjectTemplate.opsForValue().get(RedisKey.USER_PERMISSION_LIST2(accountId));
                System.out.println("需要的资源是"+requirePermission);
                System.out.println("拥有的资源是"+permissions);
                if(!permissions.contains(requirePermission)){
                    throw new Exception("资源权限不足");
                }
                return true;  // 权限匹配，放行
            }

            // 如果角色不匹配，且没有权限，则抛出异常
            throw new Exception("角色不符合要求");
        }

    // 如果没有 RequireRole 注解，直接放行
        return true;
    }
}
